// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package dengues.system;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-14 12:05:08
 */
public class ParserUtils {

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "formatNumber".
     * 
     * @param s
     * @param thousandsSeparator
     * @return
     */
    private static java.lang.String formatNumber(String s, char thousandsSeparator) {

        StringBuilder sb = new StringBuilder(s);
        int index = sb.indexOf(".");
        if (index == -1) {
            index = sb.length();
        }

        index = index - 3;
        while (index > 0 && sb.charAt(index - 1) != '-') {
            sb.insert(index, thousandsSeparator);
            index = index - 3;
        }

        return sb.toString();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "format_Number".
     * 
     * @param s
     * @param thousandsSeparator
     * @param decimalSeparator
     * @return
     */
    public static java.lang.String format_Number(String s, Character thousandsSeparator, Character decimalSeparator) {
        if (s == null) {
            return null;
        }
        String result = s;
        if (thousandsSeparator != null) {
            result = formatNumber(s, thousandsSeparator);
        }
        if (decimalSeparator != null) {
            result = result.replace('.', decimalSeparator);
        }
        return result;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "replaceSpecialCharForXML".
     * 
     * @param input
     * @return
     */
    public static java.lang.String replaceSpecialCharForXML(String input) {
        input = input.replaceAll("&", "&amp;");
        input = input.replaceAll("<", "&lt;");
        input = input.replaceAll(">", "&gt;");
        input = input.replaceAll("'", "&apos;");
        input = input.replaceAll("\"", "&quot;");
        return input;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_List".
     * 
     * @param s
     * @return
     */
    public static java.util.List parseTo_List(String s) {
        if (s != null) {
            java.util.List list = new java.util.ArrayList();
            list.add(s);
            return list;
        }
        return null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_Character".
     * 
     * @param s
     * @return
     */
    public static char parseTo_Character(String s) {
        return (s != null) ? s.charAt(0) : null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_char".
     * 
     * @param s
     * @return
     */
    public static char parseTo_char(String s) {
        return (s != null) ? s.charAt(0) : null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_Byte".
     * 
     * @param s
     * @return
     */
    public static byte parseTo_Byte(String s) {
        return Byte.decode(s).byteValue();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_byte".
     * 
     * @param s
     * @return
     */
    public static byte parseTo_byte(String s) {
        return Byte.decode(s).byteValue();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_Double".
     * 
     * @param s
     * @return
     */
    public static Double parseTo_Double(String s) {
        return Double.parseDouble(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_double".
     * 
     * @param s
     * @return
     */
    public static double parseTo_double(String s) {
        return Double.parseDouble(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_float".
     * 
     * @param s
     * @return
     */
    public static float parseTo_float(String s) {
        return Float.parseFloat(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_Float".
     * 
     * @param s
     * @return
     */
    public static Float parseTo_Float(String s) {
        return Float.parseFloat(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_int".
     * 
     * @param s
     * @return
     */
    public static int parseTo_int(String s) {
        return Integer.parseInt(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_Integer".
     * 
     * @param s
     * @return
     */
    public static Integer parseTo_Integer(String s) {
        return Integer.parseInt(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_short".
     * 
     * @param s
     * @return
     */
    public static short parseTo_short(String s) {
        return Short.parseShort(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_Short".
     * 
     * @param s
     * @return
     */
    public static java.lang.Short parseTo_Short(String s) {
        return Short.parseShort(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_long".
     * 
     * @param s
     * @return
     */
    public static long parseTo_long(String s) {
        return Long.parseLong(s);
    }

    public static java.lang.Long parseTo_Long(String s) {
        return Long.parseLong(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_Boolean".
     * 
     * @param s
     * @return
     */
    public static Boolean parseTo_Boolean(String s) {
        if (s.equals("1")) {
            return Boolean.parseBoolean("true");
        }
        return Boolean.parseBoolean(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_boolean".
     * 
     * @param s
     * @return
     */
    public static boolean parseTo_boolean(String s) {
        if (s.equals("1")) {
            return Boolean.parseBoolean("true");
        }
        return Boolean.parseBoolean(s);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_String".
     * 
     * @param s
     * @return
     */
    public static java.lang.String parseTo_String(String s) {
        return s;
    }

    public final static String DEFALUT_PATTERN = "yyyy-MM-dd\'T\'HH:mm:ss.SSSZ";

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "parseTo_Date".
     * 
     * @param s
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static java.util.Date parseTo_Date(String s, String pattern) throws java.text.ParseException {
        // check the parameter for supporting " ","2007-09-13"," 2007-09-13 "
        if (s != null) {
            s = s.trim();
        }
        if (s == null || s.length() == 0) {
            return null;
        }
        java.util.Date date = null;
        if (pattern == null) {
            pattern = DEFALUT_PATTERN;
        }
        date = new java.text.SimpleDateFormat(pattern).parse(s);
        return date;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getCurrentTime".
     * 
     * @return
     */
    public static String getCurrentTime() {
        String d = "";
        d += java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
        d += "-";
        d += java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
        d += "-";
        d += java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
        d += " ";
        d += java.util.Calendar.getInstance().get(java.util.Calendar.HOUR_OF_DAY);
        d += ":";
        d += java.util.Calendar.getInstance().get(java.util.Calendar.MINUTE);
        d += ":";
        d += java.util.Calendar.getInstance().get(java.util.Calendar.SECOND);
        d += ".";
        d += java.util.Calendar.getInstance().get(java.util.Calendar.MILLISECOND);
        return d;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "saveLog".
     * 
     * @param sb
     * @throws IOException
     */
    public static void saveLog(String fileName, StringBuilder sb) throws java.io.IOException {
        fileName += java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
        fileName += java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
        fileName += java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
        java.io.FileWriter writer = new java.io.FileWriter(fileName, true);
        writer.write(sb.toString());
        writer.flush();
        writer.close();
    }
}
